package com.rocksdata.mc.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ctz.commonframe.constant.DeviceInfo;
import com.ctz.commonframe.ui.TitleBarActivity;
import com.ctz.commonframe.ui.view.HorizontalListView;
import com.ctz.commonframe.ui.view.NoScrollViewPager;
import com.ctz.commonframe.ui.widget.AbsAdapter;
import com.ctz.commonframe.ui.widget.AdapterViewHolder;
import com.ctz.commonframe.ui.widget.ViewPagerFragmentAdapter;
import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.hintview.ColorPointHintView;
import com.rocksdata.mc.entity.BannerInfo;
import com.rocksdata.mc.Config;
import com.rocksdata.mc.Constant;
import com.rocksdata.mc.R;
import com.rocksdata.mc.business.BusinessInterface;
import com.rocksdata.mc.business.BusinessManager;
import com.rocksdata.mc.entity.NewsTagInfo;
import com.rocksdata.mc.ui.adapter.BannerListAdapter;
import com.rocksdata.mc.ui.fragment.NewsFragment;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 陈堂正 on 2017/4/7.
 */
@ContentView(R.layout.activity_news)
public class NewsActivity extends TitleBarActivity {
    private static final int REQUEST_CODE_TAG_SETTINGS = 100;

    @ViewInject(R.id.hlv_news_tag)
    private HorizontalListView hlvTag;
    @ViewInject(R.id.iv_news_tag_settings)
    private ImageView ivTagSettings;
    @ViewInject(R.id.rpv_news)
    private RollPagerView rpvNews;
    @ViewInject(R.id.vp_news)
    private NoScrollViewPager vpNews;

    private List<NewsTagInfo> newsTagList = new ArrayList<>();
    private List<NewsTagInfo> selectedNewsTagList = new ArrayList<>();
    private NewsTagListAdapter tagAdapter;
    private List<BannerInfo> newsBannerList = new ArrayList<>();
    private BannerListAdapter bannerAdapter;
    private List<Fragment> fragmentList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitleMiddleText("校内新闻");
        getNewsInfo();
    }

    private void getNewsInfo(){
//        if(Config.isDebug){
//            newsTagList.add(new NewsTagInfo("快讯", true));
//            newsTagList.add(new NewsTagInfo("通知公告", true));
//            newsTagList.add(new NewsTagInfo("部门公告", true));
//            newsTagList.add(new NewsTagInfo("学校公告", true));
//            newsTagList.add(new NewsTagInfo("新闻", true));
//
//            showSelectedTag();
//
//            if(newsBannerList.size() == 0) {
//                newsBannerList.add(new BannerInfo(R.drawable.portal1));
//                newsBannerList.add(new BannerInfo(R.drawable.portal2));
//                newsBannerList.add(new BannerInfo(R.drawable.portal3));
//                newsBannerList.add(new BannerInfo(R.drawable.portal4));
//
//                rpvNews.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, DeviceInfo.screenWidth / 2));
//                rpvNews.setHintView(new ColorPointHintView(this, Color.WHITE, Color.BLACK));
//                rpvNews.setPlayDelay(3000);
//                rpvNews.setAnimationDurtion(500);
//                bannerAdapter = new BannerListAdapter(this, newsBannerList);
//                rpvNews.setAdapter(bannerAdapter);
//            }
//        }else{
            BusinessManager.getNewsManager().getNewsTags(new BusinessInterface.OnBusinessResultListener() {
                @Override
                public void onBusinessResult(boolean isSuccess, Object object, String message) {
                    if(isSuccess){
                        newsTagList = (List<NewsTagInfo>) object;
                        tagAdapter = new NewsTagListAdapter(NewsActivity.this, newsTagList);
                        hlvTag.setAdapter(tagAdapter);
                        initViewPager();
                    }
                }
            });
//        }
    }

    private void showSelectedTag(){
        selectedNewsTagList.clear();
        for(NewsTagInfo bean: newsTagList){
            if(bean.isSelected()){
                selectedNewsTagList.add(bean);
            }
        }
        if(tagAdapter == null) {
            tagAdapter = new NewsTagListAdapter(this, selectedNewsTagList);
            hlvTag.setAdapter(tagAdapter);
        }else{
            tagAdapter.notifyDataSetChanged();
        }
        initViewPager();
    }

    private void initViewPager(){
        fragmentList.clear();
        for(NewsTagInfo bean: newsTagList){
            NewsFragment fragment = new NewsFragment();
            Bundle bundle = new Bundle();
            bundle.putString(Constant.EXTRA_KEY_NEWS_TAG, bean.getProgramCode());
            fragment.setArguments(bundle);
            fragmentList.add(fragment);
        }
        vpNews.setOffscreenPageLimit(newsTagList.size());
        vpNews.setAdapter(new ViewPagerFragmentAdapter(getSupportFragmentManager(), fragmentList));
    }

    @Event(value = {R.id.hlv_news_tag}, type = AdapterView.OnItemClickListener.class)
    private void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        tagAdapter.setSelection(position);
        vpNews.setCurrentItem(position);
    }

    @Event(value = {R.id.iv_news_tag_settings}, type = View.OnClickListener.class)
    private void onClick(View view) {
        Intent intent = new Intent(NewsActivity.this, NewsTagSettingsActivity.class);
        intent.putExtra(Constant.EXTRA_KEY_NEWS_TAG, (Serializable) newsTagList);
        startActivityForResult(intent, REQUEST_CODE_TAG_SETTINGS);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(resultCode == RESULT_OK){
            switch (requestCode) {
                case REQUEST_CODE_TAG_SETTINGS:
                    List<NewsTagInfo> list = (List<NewsTagInfo>) data.getSerializableExtra(Constant.EXTRA_KEY_NEWS_TAG);
                    newsTagList = list;
                    showSelectedTag();
                    break;
            }
        }
    }

    class NewsTagListAdapter extends AbsAdapter<NewsTagInfo>{
        private int currentPosition;

        public NewsTagListAdapter(Context context, List<NewsTagInfo> datas) {
            super(context, datas, R.layout.item_news_tag_list);
        }

        @Override
        public void convert(AdapterViewHolder holder, NewsTagInfo data) {
            TextView tvTag = holder.getView(R.id.tv_news_tag);
            tvTag.setText(data.getProgramName());
            if(holder.getPosition() == currentPosition){
                tvTag.setTextColor(0xFF2F9EEF);
                tvTag.setTextSize(18);
            }else{
                tvTag.setTextColor(0xFFA0A0A0);
                tvTag.setTextSize(16);
            }
        }

        public void setSelection(int position){
            currentPosition = position;
            notifyDataSetChanged();
        }
    }
}
